package service

import (
	"github.com/astaxie/beego/orm"
	"net/url"
	"strconv"
	"strings"
	"xianzhi/models"
	"xianzhi/utils"
)

// 获取微信菜单信息
func GetWechatMenuInfo(p map[string]interface{}) *models.WechatMenu {
	o := orm.NewOrm()
	wechatMenu := &models.WechatMenu{}
	qs := o.QueryTable("WechatMenu")
	if _, ok := p["relation"]; ok && p["relation"].(bool) {
		qs = qs.RelatedSel()
	}
	if _, ok := p["id"]; ok {
		qs = qs.Filter("id", p["id"])
	}
	qs.One(wechatMenu)
	if _, ok := p["convert"]; ok && p["convert"].(bool) {
		if wechatMenu.Id > 0 {
			wechatMenu.TypeShow = models.WechatMenuType[wechatMenu.Type]
		}
	}
	return wechatMenu
}

// 添加微信菜单
func AddWechatMenu(input url.Values) (int64, error) {
	o := orm.NewOrm()
	wechatMenu := &models.WechatMenu{}
	parentMenu := &models.WechatMenu{}
	if _, ok := input["parent_id"]; ok {
		parentId := utils.Atoi64(input["parent_id"][0])
		if parentId > 0 {
			parentMenu.Id = parentId
			o.Read(parentMenu)
		}
	}
	wechatMenu.Parent = parentMenu
	if _, ok := input["type"]; ok {
		wechatMenu.Type, _ = strconv.Atoi(input["type"][0])
	}
	if _, ok := input["name"]; ok {
		wechatMenu.Name = input["name"][0]
	}
	if _, ok := input["key"]; ok {
		wechatMenu.Key = input["key"][0]
	}
	if _, ok := input["url"]; ok {
		wechatMenu.Url = input["url"][0]
	}
	if _, ok := input["sort"]; ok {
		wechatMenu.Sort, _ = strconv.Atoi(input["sort"][0])
	}
	return o.Insert(wechatMenu)
}

// 修改微信菜单信息
func EditWechatMenu(input url.Values) (int64, error) {
	o := orm.NewOrm()
	p := orm.Params{}
	for k, v := range input {
		if k == "id" || k == "_xsrf" {
			continue
		}
		p[k] = v[0]
	}
	return o.QueryTable("WechatMenu").Filter("id", input["id"][0]).Update(p)
}

// 获取微信菜单列表
func GetWechatMenuList(p map[string]interface{}) (int64, []*models.WechatMenu) {
	var wechatMenuList []*models.WechatMenu
	o := orm.NewOrm()
	qs := o.QueryTable("WechatMenu")
	if _, ok := p["relation"]; ok && p["relation"].(bool) {
		qs = qs.RelatedSel()
	}
	if _, ok := p["parentId"]; ok {
		qs = qs.Filter("parent_id", p["parentId"])
	}
	totalCount, _ := qs.Count()
	curPage, ok := p["curPage"]
	perCount, _ := p["perCount"]
	if ok {
		start := perCount.(int) * (curPage.(int) - 1)
		qs = qs.Limit(perCount, start)
	}
	qs.OrderBy("sort").All(&wechatMenuList)
	if _, ok := p["convert"]; ok && p["convert"].(bool) {
		for _, wechatMenu := range wechatMenuList {
			wechatMenu.TypeShow = models.WechatMenuType[wechatMenu.Type]
		}
	}
	return totalCount, wechatMenuList
}

func DelWechatMenu(p map[string]interface{}) (int64, error) {
	if len(p) <= 0 {
		return 0, nil
	}
	o := orm.NewOrm()
	qs := o.QueryTable("WechatMenu")
	if _, ok := p["ids"]; ok {
		qs = qs.Filter("id__in", strings.Split(p["ids"].(string), ","))
	}
	return qs.Delete()
}

func GetAllWechatMenuList() []*models.WechatMenu {
	p := map[string]interface{}{}
	p["parentId"] = 0
	p["convert"] = true
	_, oneLevelMenuList := GetWechatMenuList(p)
	for _, oneLevelMenu := range oneLevelMenuList {
		p["parentId"] = oneLevelMenu.Id
		_, oneLevelMenu.Children = GetWechatMenuList(p)
	}
	return oneLevelMenuList
}
